﻿public interface IShape
{
    double Area { get; }
    bool IsValid { get; }
}

public abstract class Shape : IShape
{
    public abstract double Area { get; }
    public abstract bool IsValid { get; }
}
public class Rectangle : Shape
{
    private double length;
    private double width;

    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }

    public override double Area => length * width;

    public override bool IsValid => length > 0 && width > 0;
}

public class Square : Shape
{
    private double side;

    public Square(double side)
    {
        this.side = side;
    }

    public override double Area => side * side;

    public override bool IsValid => side > 0;
}

public class Triangle : Shape
{
    private double baseLength;
    private double height;

    public Triangle(double baseLength, double height)
    {
        this.baseLength = baseLength;
        this.height = height;
    }

    public override double Area => baseLength * height / 2;

    public override bool IsValid => baseLength > 0 && height > 0;
}



public static class ShapeFactory//工厂
{
    private static readonly Random random = new Random();

    public static IShape CreateShape()
    {
        //这里是对工厂产出何种产品进行设定的，我们采用random来模拟
        int shapeType = random.Next(1, 4); // 1 for Rectangle, 2 for Square, 3 for Triangle
        switch (shapeType)
        {//作业要求中为随机创建
            case 1:
                // 假设长方形的长和宽在1到10之间
                double length = random.NextDouble() * 10 + 1;
                double width = random.NextDouble() * 10 + 1;
                return new Rectangle(length, width);
            case 2:
                // 假设正方形的边长在1到10之间
                double side = random.NextDouble() * 10 + 1;
                return new Square(side);
            case 3:
                // 假设三角形的底和高在1到10之间
                double baseLength = random.NextDouble() * 10 + 1;
                double height = random.NextDouble() * 10 + 1;
                return new Triangle(baseLength, height);
            default:
                throw new ArgumentException("Invalid shape type.");
        }
    }
}


class Program
{
    static void Main(string[] args)
    {
        List<IShape> shapes = new List<IShape>();
        for (int i = 0; i < 10; i++)
        {
            IShape shape = ShapeFactory.CreateShape();
            if (shape.IsValid)
            {
                shapes.Add(shape);
                Console.WriteLine($"{i + 1}: {shape.GetType().Name}, Area: {shape.Area}");
            }
            else
            {
                Console.WriteLine($"{i + 1} 不合法.");
            }
        }

        double totalArea = shapes.Sum(s => s.Area);
        Console.WriteLine($"Total area of all shapes: {totalArea}");
    }
}




























